# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

"""This sample demonstrates how to use Gemini VertexAI to describe and draw images."""

import asyncio
import base64
from io import BytesIO

from PIL import Image

from genkit.ai import Genkit
from genkit.plugins.google_genai import VertexAI, vertexai_name

ai = Genkit(plugins=[VertexAI(project='', location='us-central1')])


@ai.flow()
async def draw_image_with_imagen() -> str:
    """Draw an image.

    Returns:
        The image.
    """
    config = {
        'number_of_images': 1,
        'language': 'en',
        'seed': 20,
        'add_watermark': False,
    }

    return await ai.generate(
        prompt='Draw a cat in a hat',
        model=vertexai_name('imagegeneration@006'),
        # optional config; check README for available fields
        config=config,
    )


async def main() -> None:
    """Main function."""
    # Imagen draws an image by description. The model used is available only in
    # VertexAI API.
    result = await draw_image_with_imagen()
    decoded_image = BytesIO(base64.b64decode(result.message.content[0].root.media.url))
    image = Image.open(decoded_image)
    image.show('Image generated by Gemini')


if __name__ == '__main__':
    ai.run_main(main())
